//
//  KKSearchBar.m
//  KKSearchBar
//
//  Created by Leo on 15/6/17.
//  Copyright (c) 2015年 Leo. All rights reserved.
//

#import "KKSearchBar.h"

@interface KKSearchTextField : UITextField

@end

@implementation KKSearchTextField

#pragma mark - Private methods

- (CGRect)textRectForBounds:(CGRect)bounds {
    
    return CGRectMake(CGRectGetMinX(bounds) + 28 , CGRectGetMinY(bounds) , CGRectGetWidth(bounds) - 28, CGRectGetHeight(bounds));
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    
    return CGRectMake(CGRectGetMinX(bounds) + 28 , CGRectGetMinY(bounds) , CGRectGetWidth(bounds) - 28, CGRectGetHeight(bounds));
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
    
    return CGRectMake(CGRectGetMinX(bounds) + 28 , CGRectGetMinY(bounds) , CGRectGetWidth(bounds) - 28, CGRectGetHeight(bounds));
}

@end

@interface KKSearchBar() <UITextFieldDelegate> {
    
    KKSearchTextField *_textField;
    CGPoint _editPoint;
    CGPoint _normalPoint;
    UIImageView* _tipImageView;
}

@end

@implementation KKSearchBar

- (void)awakeFromNib {
    
    [self initSubViewsUI];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        
        [self initSubViewsUI];
    }
    return self;
}

#pragma mark - Private methods

- (void)initSubViewsUI {
    
    self.backgroundColor = [UIColor colorWithWhite: 0.95 alpha: 1.0];
    
    _textField = [[KKSearchTextField alloc] initWithFrame: CGRectMake(10, (CGRectGetHeight(self.bounds) - 30)/2, CGRectGetWidth(self.bounds) - 20, 30)];
    _textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.returnKeyType = UIReturnKeySearch;
    _textField.font = [UIFont systemFontOfSize: 15.0];
    _textField.delegate = self;
    _textField.textAlignment = NSTextAlignmentCenter;
    _textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    _normalPoint = CGPointMake(CGRectGetWidth(_textField.bounds)/2, CGRectGetHeight(_textField.bounds)/2);
    _editPoint = CGPointMake(15, CGRectGetHeight(_textField.bounds)/2);
    
    _tipImageView = [[UIImageView alloc] initWithFrame: CGRectMake(8, 8, 14, 14)];
    _tipImageView.image = [UIImage imageNamed: @"search_bar_item"];
    _tipImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    [_textField addSubview: _tipImageView];
    [self addSubview: _textField];
    _tipImageView.center = _normalPoint;
}

#pragma mark - Public methods

- (void)setTitle:(NSString *)title {
    _textField.text = title;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _textField.placeholder = placeholder;
    
    CGFloat width = [AppUtils textWidthFromTextString: placeholder height: 20 fontSize: 16];
    
    _normalPoint = CGPointMake(CGRectGetWidth(_textField.bounds)/2 - width/2, CGRectGetHeight(_textField.bounds)/2);
    _tipImageView.center = _normalPoint;
}

#pragma mark - UITextFieldDelegate methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (CGPointEqualToPoint(_tipImageView.center, _normalPoint) && textField.text.length == 0) {
        
        _textField.textAlignment = NSTextAlignmentLeft;
        __weak UIImageView* weakTipsImageView = _tipImageView;
        [UIView animateWithDuration: 0.25 animations:^{
            weakTipsImageView.center = _editPoint;
        }];
    }
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    if (!CGPointEqualToPoint(_tipImageView.center, _normalPoint) && textField.text.length == 0) {
        
        __weak UIImageView* weakTipsImageView = _tipImageView;
        _textField.textAlignment = NSTextAlignmentCenter;
        [UIView animateWithDuration: 0.25 animations:^{
            weakTipsImageView.center = _normalPoint;
        }];
    }
    [textField resignFirstResponder];
    
    if (_searchHandle) {
        _searchHandle(textField.text);
    }
    return true;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return YES;
}
@end
